<template>
  <div class="profile-box">
    <!-- 未登录头部 -->
    <div v-if="!user"
         @click="$router.push('/login')"
         class="not-login header">
      <img src="~@/assets/mobile.png" />
      <span>登录/注册</span>
    </div>
    <!-- 已登录头部 -->
    <div v-else
         class="header login">
      <div class="userInfo">
        <div class="left">
          <van-image round
                     class="avatar"
                     :src="userInfo.photo" />
          <span class="user_name">{{userInfo.name}}</span>
        </div>
        <van-button size="mini"
                    round
                    type="default">编辑资料</van-button>
      </div>
      <!-- 用户数据 -->
      <div class="userData">
        <div class="data_item">
          <span>{{ userInfo.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="data_item">
          <span>{{ userInfo.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="data_item">
          <span>{{ userInfo.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="data_item">
          <span>{{ userInfo.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid class="nav-grid"
              :column-num="2">
      <van-grid-item>
        <i slot="icon"
           class="toutiao toutiao-shoucang"></i>
        <span class="text"
              slot="text">收 藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon"
           class="toutiao toutiao-lishi"></i>
        <span class="text"
              slot="text">历 史</span>
      </van-grid-item>
    </van-grid>
    <!-- cell结构 -->
    <div class="cell-box1">
      <van-cell title="消息通知"
                is-link></van-cell>
      <van-cell title="小智同学"
                is-link></van-cell>
    </div>
    <van-cell @click="onLogout"
              v-if="user"
              class="logout"
              title="退出登录"></van-cell>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'my',
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  async created () {
    this.loadUserInfo()
  },

  methods: {
    onLogout () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确认退出该账号？'
      })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(_ => { })
    },
    async loadUserInfo () {
      if (!this.user) return
      try {
        const { data: res } = await getUserInfo()
        this.userInfo = res.data
      } catch (err) {
        this.$toast.fail('获取数据失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.profile-box {
  .header {
    width: 750px;
    height: 401px;
    background-image: url(~@/assets/banner.png);
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      font-size: 28px;
      color: #fff;
      margin-top: 15px;
    }
  }
  .login {
    overflow: hidden;
    .userInfo {
      height: 160px;
      display: flex;
      margin-top: 80px;
      justify-content: space-between;
      align-items: center;
      padding: 0 32px;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
        }
        .user_name {
          font-size: 30px;
          margin-left: 22px;
          color: #fff;
        }
      }
    }
    .userData {
      height: 161px;
      display: flex;
      .data_item {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 18px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
    }
  }
  .nav-grid {
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .text {
      font-size: 28px;
      margin-top: 16px;
    }
  }
  .cell-box1 {
    margin: 10px 0;
  }
  .logout {
    text-align: center;
    color: red;
  }
}
</style>
